Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

Key Sequence Detection

Key Sequence Detection

[Day00] 不知不覺就要開始了呢

[Day00] 不知不覺就要開始了呢

Truthy 與 Falsy

Truthy 與 Falsy






留言討論